﻿using System;
using System.Web.Mvc;
using TimeProject.Engine;
using TimeProject.WebSite.Views;

namespace TimeProject.WebSite.Filters
{
    /// <summary>
    /// Initializes the ViewData with current user's UserInfo.
    /// </summary>
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
    public class UserInfoToViewDataAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException("filterContext");
            }

            UserInfo currentUser = RequestContext.Current.CurrentUser;
            if (currentUser == null)
            {
                // Not authorized!
                return;
            }

            filterContext.Controller.ViewData.SetUserInfo(currentUser);
        }
    }
}